﻿<%@ Control Language="C#" AutoEventWireup="true" CodeFile="RefundPays.ascx.cs" Inherits="Controls_RefundPays" %>
<%@ Register TagPrefix="ajax" Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit"%>

<asp:Button runat="server" ID="hiddenTargetControlForModalPopup" Style="display: none" />
<ajax:ModalPopupExtender 
    runat="server" 
    ID="programmaticModalPopup" 
    TargetControlID="hiddenTargetControlForModalPopup" 
    PopupControlID="programmaticPopup"
    DropShadow="True" 
    CancelControlID="infoBoxCancelButtonID"
    PopupDragHandleControlID="programmaticPopupDragHandle"
    RepositionMode="RepositionOnWindowResizeAndScroll">
</ajax:ModalPopupExtender>

<asp:Panel runat="server" ID="programmaticPopup" Style=" width:500px; height:300px; background-color: white; border: 1px solid Gray; display: none; position: absolute; top: 30%; left:30%;" >
     
    <div runat="server" ID="programmaticPopupDragHandle" style="border: 1px solid black; text-align:center; font-size:15pt; background-color:Lime; text-transform:uppercase; cursor: move; ">
        <asp:Label ID="infoBoxTitleLabel" runat="server" Text=""></asp:Label>
    </div>        

    <div runat="server" id="bodyMessageDiv">
        <asp:HiddenField ID="penaltyHiddenField" runat="server" />
        <asp:HiddenField ID="loanHiddenField" runat="server" />
        <asp:HiddenField ID="iterationHiddenField" runat="server" />
        <asp:HiddenField ID="payHiddenField" runat="server" />
        <table class="mGrid" id="tableID">
            <tr>
                <th>&nbsp;</th>
                <th>Grafic</th>
                <th>Cumulativ</th>
                <th>Curent</th>
            </tr>
            <tr>
                <td>Data platii :</td>                
                <td><asp:Label ID="refundDateLabel" runat="server" CssClass="microElement"></asp:Label></td>
                <td><asp:TextBox ID="refundDateTextBox" runat="server" CssClass="microElement" onChange="fDiffPeriod(this);"></asp:TextBox>
                    <ajax:CalendarExtender ID="refundDateCalendarExtender" runat="server"  TargetControlID="refundDateTextBox" />
                </td>
                <td><asp:Label ID="refundDaysDifferentLabel" runat="server" CssClass="microElement"></asp:Label></td>
            </tr>
            <tr>
                <td>Cursul:</td>
                <td><asp:Label ID="refundCursFirstLabel" runat="server" CssClass="microElement"></asp:Label></td>
                <td><asp:Label ID="refundCursCurentLabel" runat="server" CssClass="microElement"></asp:Label></td>
                <td><asp:TextBox ID="refundCursMixtTextBox" runat="server" CssClass="microElement" onChange="fCalculateTotals(this,1);"></asp:TextBox></td>
            </tr>            
            <tr>
                <td>Incasat :</td>
                <td><asp:Label ID="refundInputCursForingLabel" runat="server" CssClass="microElement"></asp:Label>
                    <asp:Label ID="refundInputValutaLabel" runat="server" CssClass="microElement"></asp:Label>
                </td>
                <td><asp:Label ID="refundInputPaysLabel" runat="server" CssClass="microElement"></asp:Label></td>
                <td><asp:TextBox ID="refundInputPaysTextBox" runat="server" CssClass="microElement" onChange="fCalculateTotals(this,1);"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Penalitate :</td>
                <td>&nbsp;</td>
                <td><asp:Label ID="refundPenaltyLabel" runat="server" CssClass="microElement"></asp:Label></td>
                <td><asp:TextBox ID="refundPenaltyTextBox" runat="server" MaxLength="20" CssClass="microElement" onKeyUp="fCalculateTotals(this,1);"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Dobanda :</td>
                <td><asp:Label ID="refundProfitLabel" runat="server" CssClass="microElement"></asp:Label></td>
                <td><asp:Label ID="refundProfitPaysLabel" runat="server" CssClass="microElement"></asp:Label></td>
                <td><asp:TextBox ID="refundProfitTextBox" runat="server" MaxLength="20" CssClass="microElement"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Principal :</td>
                <td><asp:Label ID="refundMainLabel" runat="server" CssClass="microElement"></asp:Label></td>
                <td><asp:Label ID="refundMainPaysLabel" runat="server" CssClass="microElement"></asp:Label></td>
                <td><asp:TextBox ID="refundMainTextBox" runat="server" MaxLength="20" CssClass="microElement"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Total catre platan :</td>
                <td><asp:Label ID="refundTotaltoPayLabel" runat="server" CssClass="microElement"></asp:Label></td>
                <td><asp:Label ID="refundTotalPaysLabel" runat="server" CssClass="microElement"></asp:Label></td>
                <td><asp:TextBox ID="refundTotalToPayTextBox" runat="server" CssClass="microElement"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Diferenta :</td>
                <td><asp:Label ID="refundDifferenceToPayLabel" runat="server" CssClass="microElement"></asp:Label></td>
                <td><asp:Label ID="refundDifferenceLabel" runat="server" CssClass="microElement"></asp:Label></td>
                <td><asp:TextBox ID="refundDifferenceTextBox" runat="server" CssClass="microElement"></asp:TextBox></td>
            </tr>
        </table>
         
    </div>   

    <div style="clear:both;"></div>  
    <div style="text-align:center;" >
        <asp:Button ID="infoBoxOkButton" runat="server" Text="Ok" Width="150px" OnClick="saveButton_Click" />
        <asp:Button ID="infoBoxCancelButtonID" runat="server" Text="Cancel" Width="150px" OnClick="cancelButton_Click" />
    </div>

    <script type="text/javascript" language="javascript">
        var newLine = '\n\r';
        var isDebugD = false;    //fDiffPeriod
        var isDebugF = false;    //fGetValueFromCell
        var isDebugC = false;    //fCalculateTotals
        var isDebugP = false;    //fParseFloat
        var isDebugS = false;    //fSetCellValue
        var isDebugV = false;    //fCalcCurs

        var tagSpan = 0;
        var tagInput = 1;

        function fDiffPeriod(t) {
            var grid = document.getElementById('tableID');

            var msgD = 'Start DiffPeriod' + newLine;
            
            var indexRow = t.parentNode.parentNode.rowIndex;
            msgD += 'indexRow: ' + indexRow + newLine;

            var getDate1 = grid.rows[indexRow].cells[1].getElementsByTagName('span')[0].innerHTML;
            msgD += 'getDate1: ' + getDate1 + newLine;

            var getDate2 = grid.rows[indexRow].cells[2].getElementsByTagName('input')[0].value;
            msgD += 'getDate2: ' + getDate2 + newLine;

            var t1 = new Date(getDate1.replace(/(\d{2})\.(\d{2})\.(\d{4})/, '$3-$2-$1'));
            msgD += 't1: ' + t1 + newLine;

            var t2 = new Date(getDate2.replace(/(\d{2})\.(\d{2})\.(\d{4})/, '$3-$2-$1'));
            msgD += 't2: ' + t2 + newLine;

            var difPeriod = parseInt((t2 - t1) / (24 * 3600 * 1000));
            msgD += 'difPeriod: ' + difPeriod + newLine;

            if (isDebugD) {
                alert(msgD);
            }

            fSetValueToCell(indexRow, 3, 0, difPeriod);
            fCalculateTotals(grid, 0);
        }
     
        function fSetValueToCell(indexRow, colIndex, typeTag, value) {
            var grid = document.getElementById('tableID');
            var msgS = 'Start fSetValueToCell';
            
            switch (typeTag) {
                case 0:
                    msgS += 'Case 0' + newLine;
                    
                    grid.rows[indexRow].cells[colIndex].getElementsByTagName('span')[0].innerHTML = roundNumber(value, 2);
                    break;
                case 1:
                    msgS += 'Case 1' + newLine;
                    grid.rows[indexRow].cells[colIndex].getElementsByTagName('input')[0].value = roundNumber(value, 2);
                    break;
            }

            if (isDebugS) {
                alert(msgS);
            }
        }

        function roundNumber(num, dec) {
            var result = Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
            //alert(num + ' ' + result);
            return result;
        }
                
        function fCalculateTotals(t, indexCell) {

            var indexRow = t.parentNode.parentNode.rowIndex;

            var cursValue = fParseFloat(document.getElementById('<%= refundCursMixtTextBox.ClientID %>').value);

            var firstForingValue = fParseFloat(document.getElementById('<%= refundInputCursForingLabel.ClientID %>').innerHTML);

            var inputCurent = fParseFloat(document.getElementById('<%= refundInputPaysTextBox.ClientID %>').value);
            
            var profitCurent = fParseFloat(document.getElementById('<%= refundProfitTextBox.ClientID %>').value);

           var penalitateCurent = parseFloat(0.00);

           var penaltyRate = fParseFloat(document.getElementById('<%= penaltyHiddenField.ClientID %>').innerHTML);

            var delayDaysCount = fParseFloat(document.getElementById('<%= refundDaysDifferentLabel.ClientID %>').innerHTML);

           var profitGrafic = fParseFloat(document.getElementById('<%= refundProfitLabel.ClientID %>').innerHTML);
           var profitCumulativ = fParseFloat(document.getElementById('<%= refundProfitPaysLabel.ClientID %>').innerHTML);

           var mainGrafic = cursValue * fParseFloat(document.getElementById('<%= refundMainLabel.ClientID %>').innerHTML);
           var mainCumulativ = cursValue * fParseFloat(document.getElementById('<%= refundMainPaysLabel.ClientID %>').innerHTML);

           switch (indexCell) {
               case 0:
                   penalitateCurent = (((parseFloat(profitGrafic) + parseFloat(mainGrafic)) * parseFloat(penaltyRate)) / 100) * parseFloat(delayDaysCount);
                   break;

               case 1:
                   penalitateCurent = fParseFloat(document.getElementById('<%= refundPenaltyTextBox.ClientID %>').value);
                    break;
            }

            if (parseFloat(penalitateCurent) < 0) { penalitateCurent = 0; }

            var profitDiff = profitGrafic - profitCumulativ;

            if (profitDiff < 0) { profitDiff = 0; }

            var mainCurent = parseFloat(0.00);
            var avansCurent = parseFloat(0.00);
           
            if (profitDiff <= inputCurent) {

                profitCurent = profitDiff;

                mainCurent = inputCurent - penalitateCurent - profitCurent;
                if (mainCurent > mainGrafic - mainCumulativ) {
                    mainCurent = mainGrafic - mainCumulativ;
                }
                avansCurent = inputCurent - penalitateCurent - profitCurent - mainCurent;
            }
            else {
               
                profitCurent = inputCurent;
            }

            document.getElementById('<%= refundPenaltyTextBox.ClientID %>').value = penalitateCurent;

            document.getElementById('<%= refundProfitTextBox.ClientID %>').value = profitCurent;

            document.getElementById('<%= refundMainTextBox.ClientID %>').value = mainCurent;

            document.getElementById('<%= refundTotalToPayTextBox.ClientID %>').value = parseFloat(penalitateCurent) + (parseFloat(profitCurent)) + (parseFloat(mainCurent));

            document.getElementById('<%= refundDifferenceTextBox.ClientID %>').value = avansCurent;

            document.getElementById('<%= refundInputPaysTextBox.ClientID %>').innerHTML = firstForingValue * cursValue;
           
            document.getElementById('<%= refundDifferenceToPayLabel.ClientID %>').innerHTML = firstForingValue * cursValue - fParseFloat(document.getElementById('<%= refundProfitLabel.ClientID %>').innerHTML) - fParseFloat(document.getElementById('<%= refundMainLabel.ClientID %>').innerHTML);
       }

        function fParseFloat(cellValue) {
            var msgP = 'Start: Parse float function (PFF)' + newLine;
            cellValue = cellValue.replace(/,/g, '.');
            msgP += 'Input cellValue: ' + cellValue + newLine;            
            if (parseFloat(cellValue)) {
                cellValue = parseFloat(cellValue);
                msgP += 'PFF: if is parseFloat' + newLine;
                if (cellValue == NaN) {
                    msgP += 'percentValue is NaN' + newLine;
                    cellValue = parseFloat(0.00);
                }
            }
            else {
                cellValue = parseFloat(0.00);
                msgP += 'PFF: is not parseFloat, cellValue=0' + newLine;
            }

            msgP += 'End: Parse float function' + newLine;
            msgP += 'Cell Value return: ' + cellValue + newLine;
            if (isDebugP) {
                alert(msgP);
            }
            return cellValue;
        }
        </script>
</asp:Panel>